﻿package domino 
{
	import Box2D.Dynamics.b2World;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author sergiy
	 */
	public class levels
	{
		[Embed(source="levels/test.xml", mimeType="Application/Octet-stream")]
            public var levelXML2:Class;
		   
		private var figures:Array;
		private var level:int;
		private var urlLoad:URLLoader;
		private var urlReq:URLRequest;
		private var levelsDoc:XML;
		private var IO_XML:IOXML;
		private var domin:actor;
		public var dominArray:Array = new Array;
		public var coinArray:Array = new Array;
		
		private var m_world:b2World;
		private var parent:DisplayObjectContainer;
		public var dominToDelete:Array = [];
	    //private var target:int;
		private var coin:int = 0;
		public var clicks:int = 0;
		public var hintArray:Array = [];
		public var hintCoordArray:Array = [];
		public var hintDelayArray:Array = [];
		public var hintAdded:Boolean = false;
		
		public function levels(_level:int, _world:b2World, _parent:DisplayObjectContainer )
		{
			
		 parent = _parent;
		 m_world = _world;
		 level = _level;	
		 }//end
		 
		 
		 
		private function IO(path_to_xml:String):void {
		    urlReq = new URLRequest(path_to_xml);
			urlLoad = new URLLoader();
			urlLoad.addEventListener(Event.COMPLETE, completeListener);
			urlLoad.load(urlReq);
		}//end
		
		private function completeListener(e:Event):void{
			 levelsDoc = new XML(urlLoad.data);	
			 IO_XML = new IOXML(levelsDoc);
			 LoadLevel(level);
			  trace(clicks + "clicks");
			// parent.dispatchEvent(new);
			parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.LOAD_LEVEL,dominArray)); 
			dominArray=[];
		}//end
		
		public function LoadXML():void {
			
			 levelsDoc = XML(new levelXML2());
			// trace(levelsDoc.toXMLString());
			 IO_XML = new IOXML(levelsDoc);
			 LoadLevel(level);
			 LoadHints(level);
			//  trace(clicks + "clicks");
			// parent.dispatchEvent(new);
			parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.LOAD_LEVEL,dominArray)); 
			dominArray=[];
		   
			}//end
			
			private function LoadHints(lvl:int):void
			{
			switch(lvl)
			{
				case 0:
				 hintAdded = true;
				 hintArray=[];
				 hintCoordArray=[];
				 hintDelayArray=[];
				
				 hintArray.push(addHint(100, -360, 0));
				 hintCoordArray.push(new Array(350, 346, -300, 400));
				 hintDelayArray.push(new Array("2sec","0sec","2sec","0sec"));
				 
				 hintArray.push(addHint(100, -360, 1));
				 hintCoordArray.push(new Array(-5, 0, -300, 430));
				 hintDelayArray.push(new Array("2sec","0sec","2sec","0sec"));
				 
				 hintArray.push(addHint(100, -360, 2));
				 hintCoordArray.push(new Array(-200, 352, 76, 76));
				 hintDelayArray.push(new Array("2sec","0sec","2sec","0sec"));
				 
				 hintArray.push(addHint(100, -360, 3));
				 hintCoordArray.push(new Array(800, 95, -200, 234));
				 hintDelayArray.push(new Array("2sec", "0sec", "2sec", "0sec"));
				 
			/*	 hintArray.push(addHint(100, -360, 4));
				 hintCoordArray.push(new Array(800, 4, 40, 5));
				 hintDelayArray.push(new Array("2sec", "0sec", "2sec", "0sec"));*/
				 
				 hintArray.push(addHint(100, -360, 5));
				 hintCoordArray.push(new Array(-100, 363, -300, 200));
				 hintDelayArray.push(new Array("2sec", "0sec", "2sec", "0sec"));
				 
				 hintArray.push(addHint(100, -360, 6));
				 hintCoordArray.push(new Array(900, 4, -300, 270));
				 hintDelayArray.push(new Array("2sec","0sec","2sec","0sec"));
				
				/*hintArray.push(addHint(100, -360, 7));
				 hintCoordArray.push(new Array(800, 26, -600, 72));
				 hintDelayArray.push(new Array("2sec","0sec","2sec","0sec"));*/
				
				
				
				 
				 
				 
				/* parent.addChildAt(addHint(100,360,0), 2);
				 parent.addChildAt(addHint(400, 260,1), 2);
				 parent.addChildAt(addHint(400,260,2), 2);
				 parent.addChildAt(addHint(400,260,3), 2);
				 parent.addChildAt(addHint(400,260,4), 2);
				 parent.addChildAt(addHint(400,260,5), 2);*/
				
				
				 break;
				
				 case 1:
				 hintAdded = true;
				  hintArray=[];
				  hintCoordArray=[];
				  hintDelayArray=[];
				 
				  hintArray.push(addHint(100, -360, 7));
				  hintCoordArray.push(new Array(-600, 240, 900, 100));
				  hintDelayArray.push(new Array("2sec", "0sec", "2sec", "0sec"));
				  
				  hintArray.push(addHint(100, -360, 4));
				  hintCoordArray.push(new Array(800, 46, 600, 100));
				  hintDelayArray.push(new Array("2sec", "1sec", "2sec", "1sec"));
				   
				  hintArray.push(addHint(100, -360, 9));
				  hintCoordArray.push(new Array(-200, 325, -300, 376));
				  hintDelayArray.push(new Array("2sec","0sec","2sec","0sec"));
				  
				  hintArray.push(addHint(100, -360, 10));
				  hintCoordArray.push(new Array(800, 40, 600, 50));
				  hintDelayArray.push(new Array("2sec", "0sec", "2sec", "0sec"));
				  
				 break;
				 
				default:
				hintAdded = false;
				    break;
			}
			}//end
			
			private function addHint(x:Number,y:Number,type:int):MovieClip
			{
				var Hint:MovieClip;
			
			switch(type)
			 {
				case 0:
				 Hint= new hint_star(); 
				break;
				case 1:
				 Hint = new hint_star_black(); 
				break;
				case 2:
				 Hint = new hint_drag(); 
				break;
			
				case 3:
				 Hint = new hint_drop(); 
				break;
				
				case 4:
				 Hint = new hint_circle(); 
				break;
				
				case 5:
				 Hint = new hint_click(); 
				break;
				
				case 6:
				 Hint = new hint_push_rectangle(); 
				break;
				
				case 7:
				 Hint = new hint_push_circle(); 
				break;
				
				case 8:
				 Hint = new hint_drag_level3(); 
				break;
				
				case 9:
				 Hint = new hint_drop_level3(); 
				break;
				
				case 10:
				 Hint = new hint_restart(); 
				break;
				
				default:
				    break;
			}
			
			
			    Hint.selectable = false;
			    Hint.mouseEnabled = false;
				Hint.visible = false;
				Hint.y = x;
				Hint.x = y;
	            
			   return Hint;
			   
			}//end
			public function deleteHints():void
		    {
			 hintArray= [];
		     hintCoordArray = [];
		     hintDelayArray = [];
		    }//end
		
			private function addTextFormat():TextFormat
			{
				var fmt:TextFormat = new TextFormat("_sans", 12, 0x666666);
			    fmt.leftMargin = 5;
			    fmt.rightMargin = 5;
			    fmt.font = "_typewriter";
				return fmt;
			}//end
		
		public function Load():Array {
			//IO("test.xml");
			LoadXML();
		    return dominArray;
			}
	    
			
		private  function LoadLevel(num:int):void {
		 figures = [];
		 figures = IO_XML.getFigures(num);
		 dominArray = [];     
		     if (figures[0][8]){
			 coin = figures[0][8];
			 trace("coin = "+ coin);
			 }
			 clicks = figures[0][12];
			 trace(clicks + "clicks");
		      for each(var figure:Array in figures) {
				  
				  
	
				  if(figure[2] == "domino"){
				    domin = new actorDomino(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[11]);	
					dominArray.push(domin);
					domin.addEventListener(OutOfScreen.OUT_DOMIN,removeHandler);
					continue;
				  }
				  
				  if(figure[2] == "triangle"){
				    domin = new actorTriangle(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[11]);	
					dominArray.push(domin);
					domin.addEventListener(OutOfScreen.OUT_DOMIN,removeHandler);
					continue;
				  }
				  
				  if (figure[2] == "circle") {
		             domin = new actorCircle(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[11]);
					 dominArray.push(domin);
					 domin.addEventListener(OutOfScreen.OUT_DOMIN,removeHandler);
					 continue;
				  }
					 
				  if (figure[2] == "cross") {
		             domin = new actorCross(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[10],figure[11]);
					 dominArray.push(domin);
					 continue;	 
				  }
				  
				  if (figure[2] == "angle") {
		             domin = new actorAngle(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[10],figure[11]);
					 dominArray.push(domin);
					 continue;	 
				  }
				  
				  if (figure[2] == "trident") {
		             domin = new actorTrident(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[10],figure[11]);
					 dominArray.push(domin);
					 continue;	 
				  }
				  
				  if (figure[2] == "trident2") {
		             domin = new actorTrident2(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[10],figure[11]);
					 dominArray.push(domin);
					 continue;	 
				  }
				 
				  if (figure[2] == "pendulum") {
		             domin = new actorPendulum(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[10],figure[11]);
					 dominArray.push(domin);
					 continue;	 
				  }
				  
				  if (figure[2] == "pendulum2") {
		             domin = new actorPendulum2(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9],figure[10],figure[11]);
					 dominArray.push(domin);
					 continue;	 
				  }
				  
				  if (figure[2] == "coin") {
		             domin = new actorCoin(m_world, parent, new Point(figure[0], figure[1]),figure[3], figure[4], figure[5],figure[6],figure[7],figure[9]);
					 if(figure[5]!=0)
					  domin.addEventListener(takeCoin.TAKE_COIN,coinHandler);
					 else
					  domin.addEventListener(takeCoin.TAKE_COIN_BLACK,coinHandlerBlack);
					  
					  dominArray.push(domin);
					// coin = figure[8];
					 continue;
				  }
				  
				   throw new Error("Error of IO");
				
			  }
		}//end
		
    private function coinHandlerBlack(e:takeCoin):void 
		{
			dominToDelete.push(e.currentTarget);
	       // coin--;
	        parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.DELETE_COIN_BLACK, dominToDelete)); 	  
	        dominToDelete = [];
			trace("it is black ");
		}
	public function getCoins():int {
		//trace(coin);
		 return coin;	
		}//end
	private function coinHandler(e:takeCoin):void {
	  dominToDelete.push(e.currentTarget);
	  coin--;
	  parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.DELETE_COIN, dominToDelete)); 	  
	  dominToDelete = [];
	 }
	 private function removeHandler(e:OutOfScreen):void 
	 {
	  trace("delete domin");
	  dominToDelete.push(e.currentTarget);
	  parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.DELETE_DOMIN, dominToDelete));
	  dominToDelete = [];
	 }
	 
	}

}